package com.ishowcode;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Jedis_ {
    @Test
    public void con(){
        Jedis jedis = new Jedis("192.168.245.101",6379);
        //如果设置了密码
        //jedis.auth("");
        String hello_redis = jedis.ping("hello redis");
        System.out.println("返回结果:"+hello_redis);
        Set<String> keys = jedis.keys("*");
        for (String key: keys) {
            System.out.print(key+" ");
        }

        jedis.close();//关闭当前链接，并没有关闭redis;

    }
    @Test
    public void key(){
        Jedis jedis = new Jedis("192.168.245.101",6379);
        jedis.set("k1","summer1");
        jedis.set("k2","summer2");
        jedis.set("k3","summer3");
        Set<String> keys = jedis.keys("*");
        for (String key: keys) {
            System.out.print("key->"+key+" ");
        }
        System.out.println();
        System.out.println(jedis.exists("k1"));
        System.out.println(jedis.ttl("k1"));
        System.out.println(jedis.get("k1"));
        jedis.mset("k4","summer4","k5","summer5","k6","summer6");//批量设置k-v
        List<String> mget = jedis.mget("k4", "k5", "k6");
        for (String s:mget
             ) {
            System.out.print("s->"+s+" ");
        }
        jedis.close();//关闭当前链接，并没有关闭redis;
    }
    @Test
    public void list(){
        Jedis jedis = new Jedis("192.168.245.101",6379);
        jedis.lpush("name_list1","jack","tom","nono");//向左添加list数据
        List<String> name_list1 = jedis.lrange("name_list1", 0, -1);
        for (String list1:name_list1
             ) {
            System.out.print("list1->"+list1+" ");
        }
        jedis.rpush("name_list2","jack","tom","nono");//向右添加list数据
        List<String> name_list2 = jedis.lrange("name_list2", 0, -1);
        System.out.println();
        for (String list2:name_list2
        ) {
            System.out.print("list2->"+list2+" ");
        }
        jedis.close();//关闭当前链接，并没有关闭redis;
    }
    @Test
    public void set(){
        Jedis jedis = new Jedis("192.168.245.101",6379);
        jedis.sadd("city","黄冈","杭州","上海","成都");//无序集合
        Set<String> citys = jedis.smembers("city");
        for (String city:citys
             ) {
            System.out.print("city->"+city+" ");
        }
        jedis.close();//关闭当前链接，并没有关闭redis;
    }
    @Test
    public void hash(){
        Jedis jedis = new Jedis("192.168.245.101",6379);
        jedis.hset("hash1","name","summer");
        String hget = jedis.hget("hash1","name");
        System.out.println("hash1->name->"+hget);
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("job","java工程师");
        hashMap.put("age","23");
        hashMap.put("sex","man");
        jedis.hset("hash2",hashMap);
        List<String> hmget = jedis.hmget("hash2", "job", "age", "sex");
        for (String info:hmget
             ) {
            System.out.println("info->"+info+" ");
        }
        jedis.close();//关闭当前链接，并没有关闭redis;
    }
    @Test
    public void zset(){
        Jedis jedis = new Jedis("192.168.245.101",6379);
        jedis.zadd("heron",5,"关羽");//有序集合按照score排
        jedis.zadd("heron",3,"黄忠");
        jedis.zadd("heron",2,"赵云");
        jedis.zadd("heron",1,"马超");
        jedis.zadd("heron",4,"张飞");
       //Set<String> heros= jedis.zrange("heron", 0, -1);//默认从小到大
        Set<String> heros= jedis.zrevrange("heron", 0, -1);//从大到小
        for (String hero:heros
             ) {
            System.out.println("hero->"+hero);
        }
        jedis.close();//关闭当前链接，并没有关闭redis;
    }
}
